
//
//  DIYTableViewController.m
//  DFront
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "DIYTableViewController.h"
#import "PKRequestManager.h"
#import "UIImageView+WebCache.h"
#import "DFwebViewController.h"
#import "CommonTableViewCell.h"
#import "DIYTableViewCell.h"
#import "DIYModel.h"
#import "DIYwebViewController.h"
#import "MJRefresh.h"

@interface DIYTableViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, assign) int page;
@end

@implementation DIYTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataSourceArray = [NSMutableArray array];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[DIYTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:@"acell"];
    [self requestDataWithType:0];
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];
    }];
    }

- (void)requestDataWithType:(NSInteger)type
{
    if (type == 0) {
        _page = 1;
    }
    NSString *str = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/bbs/ios/list.php?bbs=diybbs&boardid=231&manuid=0&order=lastdate&page=%d&productid=0&vs=440",_page];
    [PKRequestManager requestWithType:GET urlString:str parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (type == 0) {
            [self.dataSourceArray removeAllObjects];
        }
        NSArray *listArray = dic[@"list"][@"bookList"];
        for (NSDictionary *moDic in listArray) {
            DIYModel *model = [[DIYModel alloc] init];
            [model setValuesForKeysWithDictionary:moDic];
            [_dataSourceArray addObject:model];
            
        }
        
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
    } error:^(NSError *error) {
        NSLog(@"页面请求失败");
        [self.tableView.header endRefreshing];
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DIYModel *model = _dataSourceArray[indexPath.row];
    if (model.picArray.count != 0) {
        DIYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.titleLabel.text = model.title;
        [cell.authorImg sd_setImageWithURL:[NSURL URLWithString:model.photo]];
        cell.authorNameLabel.text = model.nickname;
        NSString *str = [NSString stringWithFormat:@"%@",model.reply];
        cell.replyLabel.text = [str stringByAppendingString:@" 回复"];
        NSString *str1 = model.date;
        cell.replyTimeLabel.text = [str1 substringFromIndex:5];
        
        if (model.picArray.count == 1) {
            [cell.aImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[0]]];
            cell.bImage.image = [UIImage imageNamed:@"white"];
            cell.cImage.image = [UIImage imageNamed:@"white"];
        }
        else if(model.picArray.count == 2){
            [cell.aImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[0]]];
            [cell.bImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[1]]];
            cell.cImage.image = [UIImage imageNamed:@"white"];
        }
        else if(model.picArray.count == 3){
            [cell.aImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[0]]];
            [cell.bImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[1]]];
            [cell.cImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[2]]];
        }
        
        return cell;
    }
    else
    {
        CommonTableViewCell *acell = [tableView dequeueReusableCellWithIdentifier:@"acell"];
        acell.titleLabel.text = model.title;
        [acell.authorImg sd_setImageWithURL:[NSURL URLWithString:model.photo]];
        acell.authorNameLabel.text = model.nickname;
        NSString *str = [NSString stringWithFormat:@"%@",model.reply];
        acell.replyLabel.text = [str stringByAppendingString:@" 回复"];
        NSString *str1 = model.date;
        acell.replyTimeLabel.text = [str1 substringFromIndex:5];
        return acell;
        
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DIYwebViewController *webVC = [[DIYwebViewController alloc] init];
    DIYModel *model = _dataSourceArray[indexPath.row];
    webVC.bstr = model.subcatid;
    webVC.cstr = model.bookid;
    [self.navigationController pushViewController:webVC animated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DIYModel *model = _dataSourceArray[indexPath.row];
    if (model.picArray.count != 0) {
        return 170;
    }
    else
    {
        return 150;
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
